package com.cty.controller;

import com.cty.pojo.Menu;
import com.cty.service.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Auther: cty
 * @Date: 2020/3/29 20:30
 * @Description:
 * @version: 1.0
 */
@Controller
public class MenuController {
    @Resource
    private MenuService menuServiceImpl;

    // 显示所有菜单信息
    @RequestMapping("show")
    public String show(Model model){
        model.addAttribute("menus",this.menuServiceImpl.showAllMenu());
        return "forward:/index.jsp";
    }

    // 显示某个菜单信息
    @RequestMapping("showMenu")
    public String showMenu(int id, Model model){
        Map<String, Object> map = this.menuServiceImpl.showOneMenu(id);
        model.addAttribute("menu", map.get("menu"));
        model.addAttribute("allMenu", map.get("allMenu"));

        return "update";
    }

    @RequestMapping("updateMenu")
    public String updateMenu(Menu menu){
        int index = this.menuServiceImpl.updateMenu(menu);
        if(index>0){
            return "redirect:/show";
        }else {
            return "redirect:/showMenu?id="+menu.getId();
        }
    }
}
